//
//  DYViewController.m
//  BeanSproutEDU
//
//  Created by douya on 15/2/17.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "DYViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "ALMoviePlayerController.h"
#import "VideoModel.h"
#import "MainModel.h"
@interface DYViewController ()<ALMoviePlayerControllerDelegate>
@property(nonatomic,strong) ALMoviePlayerController *moviePlayer;

@property(nonatomic) CGRect defaultFrame;
@end

@implementation DYViewController
-(void)viewWillDisappear:(BOOL)animated
{
    [self.moviePlayer stop];
//    self.moviePlayer=nil;
}
-(id)initWithContentURL:(NSURL *)URL
{
    if (self=[super init])
    {
        //创建播放器 此时创建的播放器是没有任何控制界面的 而且此时的播放器位置也是不正确的
        self.moviePlayer = [[ALMoviePlayerController alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        self.moviePlayer.view.alpha=0.f;
        self.moviePlayer.delegate=self;//非常重要
        
        //创建播放器控制界面 并将此付给上面的播放器
        ALMoviePlayerControls *movieControls = [[ALMoviePlayerControls alloc] initWithMoviePlayer:self.moviePlayer style:ALMoviePlayerControlsStyleDefault];
        
        [movieControls setBarColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5]];//设置控制栏的背景颜色
        [movieControls setTimeRemainingDecrements:NO];//是否倒序显示播放时间
        //    movieControls.fadeDelay=2.0;//可以设置控制栏属性 几秒消失 默认是5秒
        //    movieControls.barHeight=50.f;//设置控制栏的高度 默认是ios7＋ 70  ios7- 50
        //    movieControls.seekRate=2.f;//设置快进的速率
        
        self.moviePlayer.controls=movieControls;//将控制视图加载到moviePlayer上
        [self.view addSubview:self.moviePlayer.view];//将播放器视图加载到当前的view上
        
        [self.moviePlayer setContentURL:URL];
        //通过线程延迟0.3秒再将播放器显示出来
        double delayInSeconds=0.3;
        dispatch_time_t popTime=dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self configureViewForOrientation:[UIApplication sharedApplication].statusBarOrientation];//根据状态栏的方向来确定播放器的位置以及大小
            [UIView animateWithDuration:0.3 delay:0.0 options:0 animations:^{
                self.moviePlayer.view.alpha=1.f;//通过动画慢慢的将播放器显示出来
            }completion:^(BOOL finished){
                
            }];
        });
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextVideo) name:@"nextVideo" object:nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.self.view.backgroundColor = [UIColor darkGrayColor];
    count=0;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closePlayer) name:@"closePlayer" object:nil];
    
}

-(void)closePlayer
{
    NSLog(@"关闭播放器");
    [self.moviePlayer stop];
}
-(void)nextVideo
{
    NSLog(@"下一个视频");
    if (self.videoArray==nil)
    {
        return;
    }
    //连续播放
    NSLog(@"count--%d",count);
    if (count+1==self.videoArray.count)
    {
        count=-1;
    }
    count=count+1;
    VideoModel *vModel=_videoArray[count];
    self.moviePlayer.contentURL=[NSURL URLWithString:vModel.video_url];
    
}
- (void)configureViewForOrientation:(UIInterfaceOrientation)orientation {
    CGFloat videoWidth = 0;
    CGFloat videoHeight = 0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        videoWidth = 700.f;
        videoHeight = 535.f;
    } else {
        videoWidth = self.view.frame.size.width;
        videoHeight = 220.f;
    }
    
    //calulate the frame on every rotation, so when we're returning from fullscreen mode we'll know where to position the movie plauyer
    self.defaultFrame = CGRectMake(self.view.frame.size.width/2 - videoWidth/2, self.view.frame.size.height/2 - videoHeight/2, videoWidth, videoHeight);
    
    //only manage the movie player frame when it's not in fullscreen. when in fullscreen, the frame is automatically managed
    if (self.moviePlayer.isFullscreen)
        return;
    
    //you MUST use [ALMoviePlayerController setFrame:] to adjust frame, NOT [ALMoviePlayerController.view setFrame:]
    [self.moviePlayer setFrame:self.defaultFrame];
}
//根据状态栏的方向来确定播放器的位置以及大小
//-(void)configureViewForOrientation:(UIInterfaceOrientation)orientation
//{
//    CGFloat videoWidth = 0 ;
//    CGFloat videoHeight = 0;
//    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)//判断是ipad还是iphone
//    {
//        videoWidth = 700.f;
//        videoHeight = 535.f;
//    }
//    else
//    {
//        videoWidth = self.view.frame.size.width;
//        videoHeight = 220.f;
//    }
//    //设置默认的播放器的位置及播放器大小
//    self.defaultFrame = CGRectMake(self.view.frame.size.width/2-videoWidth/2, self.view.frame.size.height/2-videoHeight/2, videoWidth, videoHeight);
//    //如果是全屏模式那么就不需要默认的frame
//    if (self.moviePlayer.isFullscreen)
//    {
//        return;
//    }
//    
//    [self.moviePlayer setFrame:self.defaultFrame];
//}

#pragma mark- DYMoviePlayerControllerDelegate
- (void)moviePlayerWillMoveFromWindow {
    //movie player must be readded to this view upon exiting fullscreen mode.
    if (![self.view.subviews containsObject:self.moviePlayer.view])
        [self.view addSubview:self.moviePlayer.view];
    
    //you MUST use [ALMoviePlayerController setFrame:] to adjust frame, NOT [ALMoviePlayerController.view setFrame:]
    [self.moviePlayer setFrame:self.defaultFrame];
}
-(void)movieTimeOut
{
    NSLog(@"movie timed out");
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
   
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self configureViewForOrientation:toInterfaceOrientation];
//    self.navigationController.navigationBarHidden=NO;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if (toInterfaceOrientation==UIInterfaceOrientationPortrait)
    {
        self.navigationController.navigationBarHidden=NO;
    }
    else
    {
        self.navigationController.navigationBarHidden=YES;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
